/*******************************************************************************
 * Copyright (c) 2010 Sylvain Sicard.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Contributors:
 *     Sylvain Sicard - initial API and implementation
 ******************************************************************************/
package com.googlecode.ssdutils.beanconverter.example.business.impl;

import java.util.LinkedList;
import java.util.List;

import com.googlecode.ssdutils.beanconverter.example.business.api.TodoListBusinessService;
import com.googlecode.ssdutils.beanconverter.example.business.model.ItemBS;
import com.googlecode.ssdutils.beanconverter.example.business.model.ToDoListBS;

/**
 * @author Sylvain Sicard
 * 
 */
public class TodoListBSImpl implements TodoListBusinessService {

	List<ToDoListBS> lists;

	public TodoListBSImpl() {
		lists = new LinkedList<ToDoListBS>();

		ToDoListBS todoList = new ToDoListBS();
		List<ItemBS> list = new LinkedList<ItemBS>();
		list.add(new ItemBS("Item 1"));
		list.add(new ItemBS("Item 2"));
		list.add(new ItemBS("Item 3"));
		list.add(new ItemBS("Item 4"));

		todoList.setName("list 1");
		todoList.setItems(list);
		lists.add(todoList);

		todoList = new ToDoListBS();
		list = new LinkedList<ItemBS>();
		list.add(new ItemBS("Item 5"));
		list.add(new ItemBS("Item 6"));
		list.add(new ItemBS("Item 7"));
		list.add(new ItemBS("Item 8"));

		todoList.setName("list 2");
		todoList.setItems(list);
		lists.add(todoList);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.ssdutils.beanconverter.example.business.api.
	 * TodoListBusinessService #getAllItems()
	 */
	public List<ItemBS> getAllItems(int listId) {
		return lists.get(listId).getItems();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.ssdutils.beanconverter.example.business.api.
	 * TodoListBusinessService #itemCount()
	 */
	public int itemCount(int listId) {
		return listId == -1 ? 0 : lists.get(listId).getItems().size();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.ssdutils.beanconverter.example.business.api.
	 * TodoListBusinessService #createItem(int, java.lang.Object)
	 */
	public void createItem(int listId, int itemId, ItemBS value) {
		List<ItemBS> list = lists.get(listId).getItems();
		list.set(itemId, value);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.ssdutils.beanconverter.example.business.api.
	 * TodoListBusinessService #getItemById(int)
	 */
	public ItemBS getItemById(int listId, int itemId) {
		return lists.get(listId).getItems().get(itemId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.ssdutils.beanconverter.example.business.api.
	 * TodoListBusinessService #deleteItem(int)
	 */
	public void deleteItem(int listId, int itemId) {
		List<ItemBS> list = lists.get(listId).getItems();
		list.remove(itemId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.ssdutils.beanconverter.example.business.api.
	 * TodoListBusinessService #createItem(java.lang.String)
	 */
	public void createItem(int listId, String string) {
		List<ItemBS> list = lists.get(listId).getItems();
		list.add(new ItemBS(string));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.ssdutils.beanconverter.example.business.api.
	 * TodoListBusinessService #listCount()
	 */
	public int listCount() {
		return lists.size();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.ssdutils.beanconverter.example.business.api.
	 * TodoListBusinessService #createList(int,
	 * com.googlecode.ssdutils.beanconverter.example.business.model.ItemBS)
	 */
	public void createList(int rowIndex, String value) {
		ToDoListBS list = new ToDoListBS();
		list.setName(value);
		lists.add(list);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.ssdutils.beanconverter.example.business.api.
	 * TodoListBusinessService #getListById(int)
	 */
	public ToDoListBS getListById(int listId) {
		return lists.get(listId);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.ssdutils.beanconverter.example.business.api.
	 * TodoListBusinessService#deleteList(int)
	 */
	public void deleteList(int selectedRow) {
		lists.remove(selectedRow);
	}

}
